<?php
//$Header: /cvsroot/tsheet/timesheet.php/simple.php,v 1.7 2005/05/23 05:39:39 vexil Exp $
error_reporting(E_ALL);
ini_set('display_errors', true);
	$project_map=array();
                // class representing a project and  a project attribute 
                class ProjectClass  {
                    var $projectId;
                    var $projectName;
					var $clientName;
					var $clientId;
					var $tasks=array();// an array with the taskId as key and an TaskClass object
					
					function ProjectClass($projectId, $projectName, $clientId, $clientName,$tasks){
					$this->projectId=$projectId;
					$this->projectName=$projectName;
					$this->clientName=$clientName;
					$this->clientId=$clientId;
					$this->tasks=$tasks;
					}
					function getProjectId(){
						return $this->projectId;
					}
					function getProjectName(){
						return $this->projectName;
					}
					function getClientId(){
						return $this->clientId;
					}
					function getClientName(){
						return $this->clientName;
					}
					function getTasks(){
						return $this->tasks;
					}
					function popTask(){
						$task=array_pop($this->tasks);
						return $task;
					}
					function addTask($taskId,$task){
						//array_push($this->tasks,$task);
						$this->tasks[$taskId]=$task;
					}
                }
                // class representing a task and  a task attribute 

				class TaskClass {
					var $projectId;
					var $taskId;
					var $taskName;
					
					function TaskClass($projectId,$taskId,$taskName){
					$this->projectId=$projectId;
					$this->taskId=$taskId;
					$this->taskName=$taskName;

					}
					function getProjectId(){
						return $this->projectId;
					}
					function getTaskId(){
						return $this->taskId;
					}
					function getTaskName(){
						return $this->taskName;
					}				
				}
				
// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
require("class.Pair.php");
if (!$authenticationManager->isLoggedIn()) 
{
    Header("Location: login.php?redirect=$_SERVER[PHP_SELF]");
    exit;
}

// Connect to database.
$dbh          = dbConnect();
$contextUser  = strtolower($_SESSION['contextUser']);
$loggedInUser = strtolower($_SESSION['loggedInUser']);

if (empty($loggedInUser))
{
    errorPage("Could not determine the logged in user");
}
if (empty($contextUser))
{
    errorPage("Could not determine the context user");
}
// Define the command menu
include("timesheet_menu.inc");

// Bug fix - we must display all projects
$proj_id = 0;

// Check project assignment.
if ($proj_id != 0) 
{ 
    // id 0 means 'All Projects'
    list($qh, $num) = dbQuery("SELECT * FROM $ASSIGNMENTS_TABLE where proj_id='$proj_id' and username='$contextUser'");
    if ($num < 1)
    {
	    errorPage("You cannot access this project, because you are not assigned to it.");
    }
}
else
{
    $task_id = 0;
}

// A useful constant:
define("A_DAY", 24 * 60 * 60);

// Get the passed date (context date)
$todayDate      = mktime(0, 0, 0,$month, $day, $year);
$todayYear      = date("Y", $todayDate);
$todayMonth     = date("n", $todayDate);
$todayDay       = date("j", $todayDate);
$dateValues     = getdate($todayDate);
$todayDayOfWeek = $dateValues["wday"];

// The day the week should start on: 0=Sunday, 1=Monday
$startDayOfWeek = getWeekStartDay();
$daysToMinus    = $todayDayOfWeek - $startDayOfWeek;
if ($daysToMinus < 0)
{
    $daysToMinus += 7;
}
// Work out the start date by minusing enough seconds to make it the start day of week
$startDate  = $todayDate - $daysToMinus * A_DAY;
$startYear  = date("Y", $startDate);
$startMonth = date("n", $startDate);
$startDay   = date("j", $startDate);

// Work out the end date by adding 7 days
$endDate    = $startDate + 7 * A_DAY;
$endYear    = date("Y", $endDate);
$endMonth   = date("n", $endDate);
$endDay     = date("j", $endDate);

// Calculate the previous week
$previousWeekDate  = $todayDate - A_DAY * 7;
$previousWeekYear  = date("Y", $previousWeekDate);
$previousWeekMonth = date("n", $previousWeekDate);
$previousWeekDay   = date("j", $previousWeekDate);

// Calculate next week
$nextWeekDate  = $todayDate + A_DAY * 7;
$nextWeekYear  = date("Y", $nextWeekDate);
$nextWeekMonth = date("n", $nextWeekDate);
$nextWeekDay   = date("j", $nextWeekDate);

//get the timeformat
list($qh2, $numq) = dbQuery("select timeformat from $CONFIG_TABLE where config_set_id = '1'");
$configData       = dbResult($qh2);

// Build the database query
$query = "SELECT date_format(start_time,'%d') AS day_of_month, ";
      
if ($configData["timeformat"] == "12")
{
    $query .= "date_format(end_time, '%l:%i%p') AS endd, date_format(start_time, '%l:%i%p') AS start, ";
}
else
{
    $query .= "date_format(end_time, '%k:%i') AS endd, date_format(start_time, '%k:%i') AS start, ";
}
       
$query .=   "unix_timestamp(end_time) - unix_timestamp(start_time) AS diff_sec, ".
            "end_time                   AS end_time_str, ".
            "start_time                 AS start_time_str, ".
            "unix_timestamp(start_time) AS start_time, ".
            "unix_timestamp(end_time)   AS end_time, ".
            "$PROJECT_TABLE.title       AS projectTitle, " .
            "$TASK_TABLE.name           AS taskName, " .
            "$TIMES_TABLE.proj_id, " .
            "$TIMES_TABLE.task_id " .
            "FROM $TIMES_TABLE, $TASK_TABLE, $PROJECT_TABLE WHERE " .
            "($TASK_TABLE.source_project = $PROJECT_TABLE.title OR $TASK_TABLE.target_project = $PROJECT_TABLE.title) AND " .
            "uid='$contextUser' AND ";
      
if ($proj_id > 0) 
{
    //otherwise want all records no matter what project
    $query .=  "$TIMES_TABLE.proj_id=$proj_id AND ";
}

$query .= "$TASK_TABLE.task_id = $TIMES_TABLE.task_id AND ".
          "((start_time >= '$startYear-$startMonth-$startDay 00:00:00' AND " .
          "start_time < '$endYear-$endMonth-$endDay 00:00:00') ".
          "OR (end_time >= '$startYear-$startMonth-$startDay 00:00:00' AND " .
          "end_time < '$endYear-$endMonth-$endDay 00:00:00') ".      
          "OR (start_time < '$startYear-$startMonth-$startDay 00:00:00' AND end_time >= '$endYear-$endMonth-$endDay 00:00:00')) ".
          "ORDER BY proj_id, start_time";

?>

<html>
<head>
<title>Simple Weekly Timesheet for <?php echo "$contextUser" ?></title>
<?php include ("header.inc");
?>
<script language="Javascript">
//alert("hello");
  //define the hash table
  var projectTasksHash = {};  
<?php
//get all of the projects and put them into the hashtable
$getProjectsQuery = "SELECT $PROJECT_TABLE.proj_id, $PROJECT_TABLE.title, $PROJECT_TABLE.client_id, ".
                          "$CLIENT_TABLE.client_id, $CLIENT_TABLE.organisation FROM ".
                          "$PROJECT_TABLE, $ASSIGNMENTS_TABLE, $CLIENT_TABLE where ".
                          "$PROJECT_TABLE.proj_id=$ASSIGNMENTS_TABLE.proj_id AND ".
                          "$ASSIGNMENTS_TABLE.username='$contextUser' AND ".
                          "$PROJECT_TABLE.client_id=$CLIENT_TABLE.client_id ".
                          "ORDER BY $CLIENT_TABLE.organisation, $PROJECT_TABLE.title";                          	  
list($qh3, $num3) = dbQuery($getProjectsQuery);
$project_map=array();
//$test=new ProjectClass($projectId, $projectName, $clientId, $clientName,$tasks);
// Iterate through results
for ($i=0; $i<$num3; $i++) {
  //get the current record
  $data = dbResult($qh3, $i);
  print("projectTasksHash['" . $data["proj_id"] . "']               = {};\n");
  print("projectTasksHash['" . $data["proj_id"] . "']['name']       = '". addslashes($data["title"]) . "';\n");
  print("projectTasksHash['" . $data["proj_id"] . "']['clientId']   = '". $data["client_id"] . "';\n");
  print("projectTasksHash['" . $data["proj_id"] . "']['clientName'] = '". addslashes($data["organisation"]) . "';\n");
  print("projectTasksHash['" . $data["proj_id"] . "']['tasks']      = {};\n"); 
  $tasks=array();
  $projet=new ProjectClass($data["proj_id"], $data["title"], $data["client_id"], $data["organisation"],$tasks);
  $project_map[ $data["proj_id"] ]=$projet;
 // array_push($project_map,$projet);
  // echo "// ".$projet->getProjectName().",  id=".$projet->getProjectId().";\n";
}
//$p=array_pop($project_map);
//echo "//".$p->getProjectName().",  id=".$p->getProjectId().";\n\n\n\n";

// Get all of the tasks and put them into the hashtable
/*
$getTasksQuery = "SELECT  $TASK_TABLE.proj_id, $TASK_TABLE.task_id, $TASK_TABLE.name". 
                 "from    $TASK_TABLE, $TASK_ASSIGNMENTS_TABLE". 
                 "where   $TASK_TABLE.task_id = $TASK_ASSIGNMENTS_TABLE.task_id".
                 "AND     $TASK_ASSIGNMENTS_TABLE.username ='$contextUser'";
				 */
$getTasksQuery = "SELECT     $PROJECT_TABLE.proj_id, $TASK_TABLE.task_id, $TASK_TABLE.name from ".
						    "$TASK_TABLE, $TASK_ASSIGNMENTS_TABLE, $PROJECT_TABLE where ".
						    "$TASK_TABLE.task_id = $TASK_ASSIGNMENTS_TABLE.task_id AND ".
						  "(($TASK_TABLE.source_project = $PROJECT_TABLE.title) or ($TASK_TABLE.target_project = $PROJECT_TABLE.title)) AND ".
						    "$TASK_ASSIGNMENTS_TABLE.username='$contextUser' ".
						    "ORDER BY $TASK_TABLE.name";

list($qh4, $num4) = dbQuery($getTasksQuery);

//print("////thp " . $getTasksQuery . "\n");

//iterate through results
for ($i=0; $i<$num4; $i++) 
{
    //get the current record
    $data = dbResult($qh4, $i);
    //print("if (projectTasksHash['" . $data["proj_id"] . "'] != null)\n");
   
	if( isset( $project_map[ $data["proj_id"] ] ) ){
		 $task=new TaskClass($data["proj_id"], $data["task_id"], $data["name"] );
		 $project_map[$data["proj_id"] ]->addTask( $data["task_id"], $task );
		 print("  projectTasksHash['" . $data["proj_id"] . "']['tasks']['" . $data["task_id"] . "'] = '" . addslashes($data["name"]) . "';\n");

	}
}
	//$p=array_pop($project_map);
	//$t= $p->popTask();
	
   // echo "//".$t->getTaskName().",  id=".$t->getTaskId().";\n\n\n\n";                     
?>

//function to populate existing rows with project and task names and select the right one in each
function populateExistingSelects() 
{  // add information into the id "project_selection"  create a new sub row
    //get the number of existing rows
    var existingRows = parseInt(document.getElementById('existingRows').value);
    //alert('There are ' + existingRows + ' existing rows');
    
    //iterate to plus one to do the additional row
    for (i=0; i<=existingRows; i++) {
      //alert('existing row ' + i);
    
      //get the project and task id for this row
      var projectId = document.getElementById('project_row' + i).value;
      var taskId = document.getElementById('task_row' + i).value;
      
      //get the selects
      var projectSelect = document.getElementById('projectSelect_row' + i);
      var taskSelect = document.getElementById('taskSelect_row' + i);
      
      //add None to the selects
      projectSelect.options[projectSelect.options.length] = new Option('None', '-1');
      taskSelect.options[taskSelect.options.length] = new Option('None', '-1');  
      
      //add the projects
      var clientId = -1;
      for (key in projectTasksHash) {
        if (projectTasksHash[key]['clientId'] != clientId) {
          projectSelect.options[projectSelect.options.length] = new Option('[' + projectTasksHash[key]['clientName'] + ']', -1);
          clientId = projectTasksHash[key]['clientId'];
        }
        
        projectSelect.options[projectSelect.options.length] = new Option(String.fromCharCode(160, 160) + projectTasksHash[key]['name'], key);
                
        if (key == projectId)
          projectSelect.options[projectSelect.options.length-1].selected = true;
      }
            
      if (projectId != -1) {
        //add the tasks
        var thisProjectTasks = projectTasksHash[projectId]['tasks'];
        for (taskKey in thisProjectTasks) {
          taskSelect.options[taskSelect.options.length] = new Option(thisProjectTasks[taskKey], taskKey);
          
          if (taskKey == taskId)
            taskSelect.options[taskSelect.options.length-1].selected = true;
        }
      }
    }
	
	
	/* 
	var main_row= document.getElementById("project_selection");
	var tr_project=document.createElement(
	main_row
	
	
	*/
}
  
  function populateTaskSelect(row, projectId, selectedTaskId) {
    //get the task select for this row
    var taskSelect = document.getElementById('taskSelect_row' + row);
    
	//alert('taskSelect:' + taskSelect);
    
	//add the tasks
    var thisProjectTasks = projectTasksHash[projectId]['tasks'];
    for (taskKey in thisProjectTasks) {
        console.error(taskKey);
      taskSelect.options[taskSelect.options.length] = new Option(thisProjectTasks[taskKey], taskKey);
      
      if (taskKey == selectedTaskId)
        taskSelect.options[taskSelect.options.length-1].selected = true;
    }  
  }
  
  function clearTaskSelect(row) {  
    taskSelect = document.getElementById('taskSelect_row' + row);
	//alert('row' + row + ' -> ' + taskSelect);
	if (taskSelect != null)
	{
		for (i=1; i<taskSelect.options.length; i++)
		{
			taskSelect.options[i] = null;
		}
		//set the length back to 1
		taskSelect.options.length = 1;

		//select the 'None' option
		taskSelect.options[0].selected = true;
		
		onChangeTaskSelectRow(row);
    } 
  }

  function rowFromIdStr(idStr) {
    var pos1 = idStr.indexOf("row") + 3;
    var pos2 = idStr.indexOf('_', pos1);
    if (pos2 == -1)
      pos2 = idStr.length;
    return parseInt(idStr.substring(pos1, pos2));    
  }

  function colFromIdStr(idStr) {
    var pos1 = idStr.indexOf("col") + 3;
    var pos2 = idStr.indexOf('_', pos1);
    if (pos2 == -1)
      pos2 = idStr.length;
    return parseInt(idStr.substring(pos1, pos2));    
  }
  
function onChangeProjectSelect(idStr) 
{
    row = rowFromIdStr(idStr);
	//alert('onChangeProjectSelect : '+ row);
    clearTaskSelect(row);

    //alert('onChangeProjectSelect : ' + row + '');	
 
    // Get the project id
    var projectSelect = document.getElementById('projectSelect_row' + row);    
    var projectId     = projectSelect.options[projectSelect.selectedIndex].value;
    
    if (projectId != -1)  
    {	
        // Populate the select with tasks for this project
        populateTaskSelect(row, projectId);
    }  
    setDirty();
}
    
function onChangeTaskSelect(idStr) {
    onChangeTaskSelectRow(rowFromIdStr(idStr));    
}
    
function onChangeTaskSelectRow(row) {
    
    taskSelect = document.getElementById('taskSelect_row' + row);
    if (taskSelect.options[0].selected == true) {
    	// Disable fields: 
        for (i=1; i<=7; i++) {
            document.getElementById('days_row' + row + '_col' + i).disabled = true;
			//alert('onChangeTaskSelectRow : ' + row + '');
        }
    }    
    else {
	
        //alert('onChangeTaskSelectRow row       : ' + row);

        // Get the total number of rows : 
        var totalRows = parseInt(document.getElementById('totalRows').value);
        //alert('change task droplist on row ' + row + ', totalRows=' + totalRows);
        
        if (row == (totalRows-1)) {
            //get the row to copy
            var tempNode = document.getElementById('row' + row);
				
            //clone the row
            var newNode = tempNode.cloneNode(true);
            
            //setup the pattern to match
            var rowRegex = new RegExp("row(\\d+)");
			
            //iterate through with dom and replace all name and id attributes with regexp
            replaceIdAndNameAttributes(newNode, rowRegex, totalRows);        
            
            //get the totals node        
            var totalsNode = document.getElementById('totalsRow');        
            
            //insert the new node before the totals node
            totalsNode.parentNode.insertBefore(newNode, totalsNode);
            
            //clear the task select        
            clearTaskSelect(totalRows);

			//increment totalRows by one
            //alert('totalRows was ' + document.getElementById('totalRows').value);
            document.getElementById('totalRows').value = parseInt(document.getElementById('totalRows').value) + 1;
            //alert('totalRows is now ' + document.getElementById('totalRows').value);        

        }
        
        // Enable fields:
        for (i=1; i<=7; i++) {
            document.getElementById('days_row' + row + '_col' + i).disabled = false;
        }
    }
   setDirty();
}
  
function replaceIdAndNameAttributes(node, rowRegex, rowNumber) 
{  
	//alert('replaceIdAndNameAttributes');
	while (node != null) {    
      if (node.getAttribute != null && node.getAttribute("id") != null)
	  {
        node.setAttribute("id", node.getAttribute("id").replace(rowRegex, "row" + rowNumber));
		//alert(node.getAttribute("id") );
	  }
      if (node.getAttribute != null && node.getAttribute("name") != null)
        node.setAttribute("name", node.getAttribute("name").replace(rowRegex, "row" + rowNumber));
        
		//call this function recursively for children
		if (node.firstChild != null /*&& node.firstChild.localName != null*/)
			replaceIdAndNameAttributes(node.firstChild, rowRegex, rowNumber);    

      //do the same for the next sibling      
      node = node.nextSibling;      
    }
  }
  
  
function recalculateRowCol(idStr) {
    recalculateRow(rowFromIdStr(idStr));
    recalculateCol(colFromIdStr(idStr));
    setDirty();
	
}

function recalculateRow(row) {
    var totalMins = 0;    
    for (i=1; i<=7; i++) {
	    days = parseInt(document.getElementById("days_row" + row + "_col" + i).value);
        if (!isNaN(days))
        {	  
            totalMins += (days * 8 * 60);
        }
    }
    days = (totalMins / 60 / 8);
    //get the total cell
    var totalCell = document.getElementById("subtotal_row" + row);

    totalCell.innerHTML = '' + days + ' days&nbsp;';
}
 
function recalculateCol(col) {  

    // Get the total number of rows:  
    var totalRows = parseInt(document.getElementById('totalRows').value);    
    var totalMins = 0;  
	
    for (i=0; i<totalRows; i++) 
	{
	    days = parseInt(document.getElementById("days_row" + i + "_col" + col).value);
        if (!isNaN(days))
	    {
            totalMins += (days * 60 * 8);
	    }
    }
    
    days = (totalMins / 60 / 8);
    
    // Get the total cell:
    var totalCell = document.getElementById("subtotal_col" + col);
    totalCell.innerHTML = '' + days + ' d&nbsp;';
    recalculateGrandTotal();
}
 
function recalculateGrandTotal() {  
    var totalMins = 0;
	
    for (i=1; i<=7; i++) 
	{
        var currentInnerHTML = document.getElementById("subtotal_col" + i).innerHTML;
		  
		//get the day
        dPos = currentInnerHTML.indexOf('d');
        days = parseInt(currentInnerHTML.substring(0, dPos));      
        if (!isNaN(days)) 
		{
          totalMins += (days * 8 * 60); 
		}
    }
	

    days = (totalMins / 60 / 8);

    //get the grand total cell
    var grandTotalCell = document.getElementById("grand_total");            
    grandTotalCell.innerHTML = '' + days + ' d&nbsp;';
}
  
function setDirty() {
    document.getElementById("saveButton").disabled = false;
}
  
function validate() {
    // Get the total number of rows:  
    var totalRows = parseInt(document.getElementById('totalRows').value);

	// Iterate through rows:
    for (i=0; i<totalRows; i++) 
	{    
        // Iterate through cols:
        for (j=1; j<=7; j++) {
/*
			hours = parseInt(document.getElementById("hours_row" + i + "_col" + j).value);
            if (document.getElementById("hours_row" + i + "_col" + j).value != "" && isNaN(hours) || hours > 23) 
			{
                alert('The hours field in row ' + i + ' column ' + j + ' must be a number between 0 and 23.');
                document.getElementById("hours_row" + i + "_col" + j).focus();
                return;
            }
  */            
            //  mins = parseInt(document.getElementById("mins_row" + i + "_col" + j).value);
            //  if (document.getElementById("mins_row" + i + "_col" + j).value != "" && isNaN(mins) || mins > 59) {
            //    alert('The minutes field in row ' + i + ' column ' + j + ' must be a number between 0 and 59.');
            //          document.getElementById("mins_row" + i + "_col" + j).focus();
            //          return;          
            //        }
			days = parseInt(document.getElementById("days_row" + i + "_col" + j).value);
            if (document.getElementById("days_row" + i + "_col" + j).value != "" && isNaN(days) || days > 1) 
			{
                alert('The days field in row ' + i + ' column ' + j + ' must be a number between 0 and 1.');
                document.getElementById("days_row" + i + "_col" + j).focus();
                return;          
            }
        }
    }
    document.theForm.submit();
  }

</script>
</head>
<?php 
echo "<body width=\"100%\" height=\"100%\" onLoad=\"populateExistingSelects();\"";
include ("body.inc");
if (isset($popup))
  echo "onLoad=window.open(\"popup.php?proj_id=$proj_id&task_id=$task_id\",\"Popup\",\"location=0,directories=no,status=no,menubar=no,resizable=1,width=420,height=205\");";
echo ">\n";

include ("banner.inc");
?>
<form name="theForm" action="simple_action.php" method="post">
    <input type="hidden" name="year"       value=<?php echo $year;       ?>>
    <input type="hidden" name="month"      value=<?php echo $month;      ?>>
    <input type="hidden" name="day"        value=<?php echo $day;        ?>>
    <input type="hidden" name="startYear"  value=<?php echo $startYear;  ?>>
    <input type="hidden" name="startMonth" value=<?php echo $startMonth; ?>>
    <input type="hidden" name="startDay"   value=<?php echo $startDay;   ?>>

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="100%" class="face_padding_cell">
                <!-- include the timesheet face up until the heading start section -->
                <table width="100%" border="0">
                    <tr>
                        <td align="left" nowrap class="outer_table_heading">
                          Week Start: <?php echo date('D F j, Y',mktime(0,0,0,$startMonth, $startDay, $startYear)); ?>
                        </td>
                        <td align="right" nowrap>
                            <a href="<?php echo $_SERVER["PHP_SELF"]; ?>?proj_id=<?php echo $proj_id; ?>&task_id=<?php echo $task_id; ?>&year=<?php echo $previousWeekYear ?>&month=<?php echo $previousWeekMonth ?>&day=<?php echo $previousWeekDay ?>" class="outer_table_action">Prev</a>
                            <a href="<?php echo $_SERVER["PHP_SELF"]; ?>?proj_id=<?php echo $proj_id; ?>&task_id=<?php echo $task_id; ?>&year=<?php echo $nextWeekYear ?>&month=<?php echo $nextWeekMonth ?>&day=<?php echo $nextWeekDay ?>" class="outer_table_action">Next</a>
                        </td>
                        <td align="right" nowrap>
                            <input type="button" name="saveButton" id="saveButton" value="Save Changes" disabled="true" onClick="validate();" />              
                        </td>
                    </tr>
                </table>
                <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
                    <tr>
                        <td>
                            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table_body">
                                <tr class="inner_table_head">
                                    <td class="inner_table_column_heading" align="center">Tasks</td>
                                    <td align="center" width="2">&nbsp;</td>
                                    <?php
                                    // Print the days of the week:
                                    $currentDayDate = $startDate;
                                    for ($i=0; $i<7; $i++) 
									{
                                        $currentDayStr   = strftime("%a", $currentDayDate);
                                        $currentDayDate += A_DAY;              
                                        print   "<td align=\"center\" width=\"65\">" .
                                                    "<table width=\"65\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" .
													    "<tr>" .
													        "<td class=\"inner_table_column_heading\" align=\"left\">" .
                                                                "$currentDayStr" .
                                                            "</td>" .
														"</tr>" .
													"</table>" .
												"</td>\n";
                                    }
                                    ?>
                                    <td align="center" width="2">&nbsp;</td>
                                    <td class="inner_table_column_heading" align="center" width="50">Total</td>
                                </tr>
                                <tr>
              <?php 
              
                //debug
                //$startDateStr = strftime("%D", $startDate);
                //$endDateStr   = strftime("%D", $endDate);      
                //print "<p>WEEK start: $startDateStr WEEK end: $endDateStr</p>";

                class TaskInfo extends Pair {
                    var $projectId;
                    var $projectTitle;
                    var $taskName;
                    
                    function TaskInfo($value1, $value2, $projectId, $projectTitle, $taskName) 
					{
                        parent::Pair($value1, $value2);
                        $this->projectId    = $projectId;
                        $this->projectTitle = $projectTitle;
                        $this->taskName     = $taskName;
                    }
                }
              
                // Get the Weekly data.
                list($qh5, $num5) = dbQuery($query);  
              
                //print "<p>Query: $query </p>";
                //print "<p>there were $num3 results</p>";
                //we're going to put the data into an array of
                //different (unique) TASKS
                //which has an array of DAYS (7) which has
                //and array of size 4: 
                // -index 0 is task entries array for tasks which started on a previous day and finish on a following day
                // -index 1 is task entries array for tasks which started on a previous day and finish today
                // -index 2 is task entreis array for tasks which started and finished today
                // -index 3 is task entries array for tasks which started today and finish on a following day
              
                $structuredArray = array();

                $previousTaskId  = -1;
                $currentTaskId   = -1;
              
                //iterate through results
                for ($i=0; $i<$num5; $i++) 
				{
                    // Get the record for this task entry
                    $data = dbResult($qh5,$i);
                    
                    // Due to a bug in mysql with converting to unix timestamp from the string, 
                    // we are going to use php's strtotime to make the timestamp from the string.
                    // the problem has something to do with timezones.
                    $data["start_time"] = strtotime($data["start_time_str"]);
                    $data["end_time"]   = strtotime($data["end_time_str"]);
                    
                    //get the current task properties
                    $currentTaskId        = $data["task_id"];
                    $currentTaskStartDate = $data["start_time"];
                    $currentTaskEndDate   = $data["end_time"];
                    $currentTaskName      = stripslashes($data["taskName"]);
                    $currentProjectTitle  = stripslashes($data["projectTitle"]);
                    $currentProjectId     = $data["proj_id"];
						
                    
                    //debug
                    //print "<p>taskId:$currentTaskId '$data[taskName]', start time:$data[start_time_str], end time:$data[end_time_str]</p>";
                    
                    
                    //find the current task id in the array
                    $taskCount = count($structuredArray);
					//echo "</br>".$taskCount."\n";
                    unset($matchedPair);
                    
					for ($j=0; $j<$taskCount; $j++) 
					{
				// $o =$structuredArray[$j];
				// echo "//debug".$o->projectId."== j ==".$j;
                        //does its value1 (the task id) match?
                        if ($structuredArray[$j]->value1 == $currentTaskId) 
						{
						//echo $structuredArray[$j]->value1;
                            //store the pair we matched with
                            $matchedPair = &$structuredArray[$j];
                            //debug        
                           // print "<p> found existing matched pair so adding to that one </p>";
                            //break since it matched
                            break;
                        }
                    }
                    
                    //was it not matched
                    if (!isset($matchedPair)) 
					{
                    
                        //debug        
                        //print "<p> creating a new matched pair for this task </p>";
                          
                        //create a new days array
                        $daysArray = array();
                        
                        //put an array in each day (this internal array will be of size 4)
                        for ($j=0; $j<7; $j++) 
						{
                            //create a task event types array
                            $taskEventTypes = array();
                            
                            //add 4 arrays to it
                            for ($k=0; $k<4; $k++)
                              $taskEventTypes[] = array();
                              
                            //add the task event types array to the days array for this day
                            $daysArray[] = $taskEventTypes;
                        }
                        //create a new pair
                        $matchedPair = new TaskInfo($currentTaskId, $daysArray, $currentProjectId, $currentProjectTitle, $currentTaskName);
                        
                        // Add the matched pair to the structured array
                        $structuredArray[] = $matchedPair;          
                        
                        // Make matchedPair be a reference to where we copied it to
                        $matchedPair = &$structuredArray[count($structuredArray)-1];      
                        
                        //print "<p> added matched pair with task '$matchedPair->taskName'</p>";
                    }
                      
                    // Iterate through the days array
                    for ($k=0; $k<7; $k++) 
					{
                        //$dayStart = strftime("%D %T", $startDate + $k * A_DAY);
                        //$dayEnd = strftime("%D %T", $startDate + ($k + 1) * A_DAY);    
                        //print "<p>DAY start: $dayStart, DAY end: $dayEnd</p>";
                        
                        //work out some booleans
                        $startsOnPreviousDay = ($currentTaskStartDate <  ($startDate +  $k * A_DAY));
                        $endsOnFollowingDay  = ($currentTaskEndDate   >= ($startDate + ($k + 1) * A_DAY));
                        $startsToday         = ($currentTaskStartDate >= ($startDate +  $k * A_DAY) && 
                                                $currentTaskStartDate <  ($startDate + ($k + 1) * A_DAY));
                        $endsToday           = ($currentTaskEndDate   >= ($startDate +  $k * A_DAY) &&
                                                $currentTaskEndDate   <  ($startDate + ($k + 1) * A_DAY));  
                        
                        //$currentTaskStartDateStr = strftime("%D %T", $currentTaskStartDate);
                        //$currentTaskEndDateStr = strftime("%D %T", $currentTaskEndDate);
                        //print "<p>task start: $currentTaskStartDateStr task end: $currentTaskEndDateStr</p>";
                                            
                        //print "<p>startsOnPreviousDay=$startsOnPreviousDay, endsOnFollowingDay=$endsOnFollowingDay" .
                        //  ", startsToday=$startsToday, endsToday=$endsToday</p>";
                        
                        //does it start before this day and end after this day?
                        if ($startsOnPreviousDay && $endsOnFollowingDay)
                          //add this task entry to the array for index 0
                          $matchedPair->value2[$k][0][] = $data;
                        //does it start before this day and end on this day?
                        else if ($startsOnPreviousDay && $endsToday)
                          //add this task entry to the arry for index 1
                          $matchedPair->value2[$k][1][] = $data;
                        //does it start and end on this day?
                        else if ($startsToday && $endsToday){
						//add this task entry to the array for index 2          
                          $matchedPair->value2[$k][2][] = $data;
						}
                        //does it start on this day and end on a following day
                        else if ($startsToday && $endsOnFollowingDay)
                          //add this task entry to the array for index 3
                          $matchedPair->value2[$k][3][] = $data;
						//echo $data["start_time"]."    ";						  
                    }
                }
                
                //by now we should have our results structured in such a way that it it easy to output it
              
                //set vars
                $previousProjectId = -1;
                $allTasksDayTotals = array(0,0,0,0,0,0,0); //totals for each day
              
             /* $previousTaskId  = -1;  
                $thisTaskId      = -1;  
                $columnDay       = -1;
                $columnStartDate = $startDate;*/
                
                //iterate through the structured array
                $count = count($structuredArray);
				//echo $count;
                unset($matchedPair);
				$i=0;
				foreach($project_map as &$project){ //=======================================> my source code
					$tasks=$project->getTasks();
					//echo " good".$project->getProjectName();
					$taskCount= count ($tasks);
					// for($x=0;$x<=$taskCount;$x++){
					// if( isset( $tasks[$x] )==true ) {
					// $task=$tasks[$x];
					// echo $task;
					// }
					foreach($tasks as&$task){
					//	echo $task->getTaskName()."#</br>";
						                        //create a new days array
                        $daysArray = array();
                        
                        //put an array in each day (this internal array will be of size 4)
                        for ($j=0; $j<7; $j++) 
						{
                            //create a task event types array
                            $taskEventTypes = array();
                            
                            //add 4 arrays to it
                            for ($k=0; $k<4; $k++)
                              $taskEventTypes[] = array();
                              
                            //add the task event types array to the days array for this day
                            $daysArray[] = $taskEventTypes;
                        }
						$contain=false;
						foreach($structuredArray as &$t){
							if (($t->taskName == $task->getTaskName())&&( $t->projectId==$task->getProjectId() ) ){
								//echo $t->projectTitle."   \n";
								$contain=true;
								break;
							}
						}
						if ($contain==true){
						
						}else if ($contain==false){
						//create a new pair
                        $matchedPair = new TaskInfo($task->getTaskId(), $daysArray, $project->getProjectId(), $project->getProjectName(), $task->getTaskName() );
						array_push($structuredArray,$matchedPair);
						
						}

					}
				}
				
				 $count = count($structuredArray);
					//echo $count;
				//loop around the structuredArray
              unset($matchedPair);

                for ($i=0; $i<$count; $i++) 
				{
                    $matchedPair = &$structuredArray[$i];
					//echo '<script type="text/javascript">' . 'alert( '.$i.' );' . '</script>';
                    ?>
				    <tr id="row<?php echo $i; ?>" debug="test_nacim">
				        <td class="calendar_cell_middle" valign="top">
				            <table width="100%" border="0" cellspacing="0" cellpadding="0">
				                <tr>
				                    <td width="50%" align="left">
									<?php  echo $matchedPair->projectTitle; ?>
									<input type="hidden" id="project_row<?php echo $i; ?>" name="project_row<?php echo $i; ?>" value="<?php  echo $matchedPair->projectId; ?>" />
				                       <div style="visibility:hidden;height:0px;"> <select  id="projectSelect_row<?php echo $i; ?>" name="projectSelect_row<?php echo $i; ?>" onChange="onChangeProjectSelect(this.id);" style="width: 100%;" /> 
				                    </td>
				                    <td width="50%" align="left">
									<?php  echo $matchedPair->taskName; ?>
				                        <input type="hidden" id="task_row<?php echo $i; ?>" name="task_row<?php echo $i; ?>" value="<?php echo $matchedPair->value1; ?>" />
										<div style="visibility:hidden;height:0px;"><select id="taskSelect_row<?php echo $i; ?>" name="taskSelect_row<?php echo $i; ?>" onChange="onChangeTaskSelect(this.id);" style="width: 100%;" /></div>
				                    </td>
				                </tr>
			                    <tr>
				                    <td height="1">
				                        <img src="images/spacer.gif" width="100" height="10" />
				                    </td>
				                </tr>
				            </table>
				        </td>
				        <?php     
                        //print the spacer column:
                        print "<td class=\"calendar_cell_disabled_middle\" width=\"2\">&nbsp;</td>";
                        //iterate through the days array:
                        $currentDay  = 0;
                        $weeklyTotal = 0;
                        foreach ($matchedPair->value2 as $currentDayArray) 
						{
                            // Open the column:
                            print "<td class=\"calendar_cell_middle\" valign=\"top\" align=\"left\">";
                            
                            // While we are printing times set the style:
                            print "<span class=\"task_time_small\">";
                                  
                            // Declare todays vars:     
                            $todaysStartTime = $startDate + $currentDay * A_DAY;
                            $todaysEndTime   = $startDate + ($currentDay + 1) * A_DAY;
                            $todaysTotal     = 0;
                            $currentDay++;
                            
                            // Create a flag for empty cell:
                            $emptyCell = true;
                            
                            // Iterate through the current day array:
                            for ($j=0; $j<4; $j++) 
							{
                                $currentTaskEntriesArray = $currentDayArray[$j];        
                                //print "C" . count($currentTaskEntriesArray) . " ";
                                //iterate through the task entries
                                foreach ($currentTaskEntriesArray as $currentTaskEntry) 
								{
                                    // is the cell empty?
                                    if ($emptyCell)
									{
                                      //the cell is not empty since we found a task entry
                                        $emptyCell = false;          
                                    } else {          
                                        //print a break for the next entry
                                        //print "<br>"; /* TEST TEST TEST TEST */
                                    } 
                                    //format printable times
                                    $formattedStartTime = $currentTaskEntry["start"];
                                    $formattedEndTime   = $currentTaskEntry["endd"];            
                                              
                                    switch($j) {
                                        case 0: //tasks which started on a previous day and finish on a following day
                                          //print "...-...";
                                          $todaysTotal += A_DAY;
                                          break;
                                        case 1: //tasks which started on a previous day and finish today
                                          //print "...-" . $formattedEndTime;
                                          $todaysTotal += $currentTaskEntry["end_time"] - $todaysStartTime;
                                          break;
                                        case 2: //tasks which started and finished today
                                          //print $formattedStartTime . "-" . $formattedEndTime;
                                          $todaysTotal += $currentTaskEntry["end_time"] - $currentTaskEntry["start_time"];
                                          break;
                                        case 3: //tasks which started today and finish on a following day
                                          //print $formattedStartTime . "-...";
                                          $todaysTotal += $todaysEndTime - $currentTaskEntry["start_time"];
                                          break;
                                        default:
                                          print "error";          
                                    }            
                                }
                            }
                                  
                            //create a string to be used in form input names
                            $rowCol = "_row" . $i . "_col" . $currentDay;
                                  
                            //make sure the cell has at least a space in it so that its rendered by the browser
                            if ($emptyCell) {
                                print "<span nowrap><input type=\"text\" id=\"days" . $rowCol . "\" name=\"days" . $rowCol . "\" size=\"5\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" />d</span>";  							  
                            }
                            else {
                                // Split todays total into hours and minutes
                                $todaysHours   = floor($todaysTotal / 60 / 60);
                                //$todaysMinutes = ($todaysTotal - ($todaysHours * 60 * 60)) / 60;
								$todaysDays     =  $todaysTotal / 60 / 60 / 8 ;
								print "<span nowrap><input type=\"text\" id=\"days" . $rowCol . "\" name=\"days" . $rowCol . "\" size=\"5\" value=\"" . $todaysDays . "\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" />d</span>";      
						   }
						   
						   
                              
                            //close the times class
                            print "</span>";
                              
                            /*if (!$emptyCell) {
                              //print todays total
                              $todaysTotalStr = formatSeconds($todaysTotal);
                              print "<br><span class=\"task_time_total_small\">$todaysTotalStr</span>";      
                            }*/
                            
                            //end the column
                            print "</td>";
                            
                            //add this days total to the weekly total
                            $weeklyTotal += $todaysTotal;
                            
                            //add this days total to the all tasks total for this day
                            $allTasksDayTotals[$currentDay - 1] += $todaysTotal;
                        }  
                        
                        //print the spacer column
                        print "<td class=\"calendar_cell_disabled_middle\" width=\"2\">&nbsp;</td>";
                        
                        //format the weekly total
                        //$weeklyTotalStr = formatSeconds($weeklyTotal);
                        $weeklyTotalStr = $weeklyTotal / 60 / 60 / 8;  
                        //print the total column
                        print "<td class=\"calendar_totals_line_weekly\" valign=\"bottom\" align=\"right\" class=\"subtotal\">";
                        print "<span class=\"calendar_total_value_weekly\" align=\"right\" id=\"subtotal_row" . $i . "\">$weeklyTotalStr d</span></td>";
                        
                        //end the row
                        print "</tr fghfghfgh>";        
                        
                        //store the previous task and project ids
                        $previousTaskId = $currentTaskId;
                        $previousProjectId = $matchedPair->projectId;
                }
                    
                /////////////////////////////////////////  
                //add an extra row for new data entry
                ///////////////////////////////////////// a voir a enlever jusqu'a
				?>
				
				<div style="visibility:hidden; display:none;"><!-- the display of the last line is displayed but it's style active in order to keep the computation ok -->
			<tr style="visibility:hidden; display:none;" id="row<?php echo $count; ?>" debug="test_loop"> 
				    <td class="calendar_cell_middle" valign="top"> 
				        <table width="100%" border="0" cellspacing="0" cellpadding="0">
				        	<tr id="project_selection">
				        	    <td width="50%" align="left"> 
				        		    <input type="hidden" id="project_row<?php echo $count; ?>" name="project_row<?php echo $count; ?>" value="-1" />
				        			<select id="projectSelect_row<?php echo $count; ?>" name="projectSelect_row<?php echo $count; ?>" onChange="onChangeProjectSelect(this.id);" style="width: 100%;" />
				        		</td>
				    			
				        		<td width="50%" align="left"> 
				        			<input type="hidden" id="task_row<?php echo $count; ?>" name="task_row<?php echo $count; ?>" value="-1" />
				        			<select id ="taskSelect_row<?php echo $count; ?>" name="taskSelect_row<?php echo $count; ?>" onChange="onChangeTaskSelect(this.id);" style="width: 100%;" />
				        		</td> 
				        	</tr>
				        	<tr>
				        		<td height="1">
				        			<img src="images/spacer.gif" width="100" height="1" />
				        		</td>
				        	</tr>
				        </table>
			        </td>
			    <?php         
				
			 //echo '<script type="text/javascript">' . 'alert("simple ligne 993" );' . '</script>';
				
                // Print the spacer column
                print "<td class=\"calendar_cell_disabled_middle\" width=\"2\">&nbsp;</td>";
                
                for ($currentDay=1; $currentDay<=7; $currentDay++) {
                    //open the column
                    print "<td class=\"calendar_cell_middle\" valign=\"top\" align=\"left\" width=\"65\">";
                    
                    //while we are printing times set the style
                    print "<span class=\"task_time_small\">";
                    
                    //create a string to be used in form input names
                    $rowCol = "_row" . $count . "_col" . $currentDay;
				    
                    print "<span nowrap><input type=\"text\" id=\"days" . $rowCol . "\" name=\"days" . $rowCol . "\" size=\"5\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" disabled=\"true\" />d</span>";     
                     
				    
                    //close the times class
                    print "</span>";
                    
                    //end the column
                    print "</td>";      
                }
                
                //print the spacer column
                print "<td class=\"calendar_cell_disabled_middle\" width=\"2\">&nbsp;</td>";
                
                //print the total column
                print "<td class=\"calendar_totals_line_weekly\" valign=\"bottom\" align=\"right\" class=\"subtotal\" width=\"50\">";
                print "<span class=\"calendar_total_value_weekly\" align=\"right\" id=\"subtotal_row" . $count . "\">0d</span></td>";
                
                //end the row
                print "</tr></div";        
                ////////////////////////////////////////////////////////////////////// <?php  
              
                
                //store a hidden form field containing the number of existing rows
                print "<input type=\"hidden\" id=\"existingRows\" name=\"existingRows\" value=\"" . $count . "\" />";
                
                //store a hidden form field containing the total number of rows
                print "<input type=\"hidden\" id=\"totalRows\" name=\"totalRows\" value=\"" . ($count + 1) . "\" />";
                
				//echo '<script type="text/javascript">' . 'alert("count : '.$count.'" );' . '</script>';
				////////////////////////////////////////////////////
                // Create a new totals row
                print "<tr id=\"totalsRow\">\n";
                print "<td class=\"calendar_cell_disabled_middle\" align=\"right\">Total Days:</td>\n";
                print "<td class=\"calendar_cell_disabled_middle\" width=\"2\">&nbsp;</td>\n";
                          
                //iterate through day totals for all tasks
                $grandTotal = 0;
                $col = 0;
                foreach ($allTasksDayTotals as $currentAllTasksDayTotal) {
                    $col++;
                    $grandTotal    += $currentAllTasksDayTotal;
                    //$formattedTotal = formatSeconds($currentAllTasksDayTotal);
					$formattedTotal = $currentAllTasksDayTotal / 60 / 60 / 8;
                    print "<td class=\"calendar_totals_line_weekly_right\" align=\"right\">\n";
                    print "<span class=\"calendar_total_value_weekly\" id=\"subtotal_col" . $col . "\">$formattedTotal d</span></td>";
                }
                
                //print grand total
                //$formattedGrandTotal = formatSeconds($grandTotal);
				$formattedGrandTotal = $grandTotal / 60 / 60 / 8;
                print "<td class=\"calendar_cell_disabled_middle\" width=\"2\">&nbsp;</td>\n";
                print "<td class=\"calendar_totals_line_monthly\" align=\"right\">\n";
                print "<span class=\"calendar_total_value_monthly\" id=\"grand_total\">$formattedGrandTotal d</span></td>";
                print "</tr>";
                  
              ?>       
                      </table>
                    </td>    
                  </tr>
                </table>
            </td>
        </tr>
    </table>
</form>
<?php include ("footer.inc");
?>
</body>
</html>

